package com.example.ui.contact.adapter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.huashangapp.R;
import com.example.ui.contact.bean.ChatMsgItem;
import com.example.ui.contact.bean.RecentItem;
import com.example.ui.contact.chat.PushMessageReceiver;
import com.example.ui.contact.chat.PushMessageReceiver.EventHandler;
import com.example.util.BitmapLoad;
import com.example.util.Util;


/**
 * 私人消息列表适配器
 * @author Leone
 *
 */
public class MsgListViewAdapter extends BaseAdapter implements EventHandler{
	private LayoutInflater inflater;
	private List<String> items;
	private Context mContext;
	private TextView titleText;
	private List<RecentItem> recent_msgList = new LinkedList<RecentItem>();// 消息对象数组
	public MsgListViewAdapter(Context mContext,TextView titleText,
			List<RecentItem> recent_msgList){
		this.mContext = mContext;
		this.titleText = titleText;
		this.recent_msgList = recent_msgList;
		inflater = LayoutInflater.from(this.mContext);
	}

	public void remove(int position) {
		if (position < recent_msgList.size()) {
			recent_msgList.remove(position);
			notifyDataSetChanged();
		}
	}

	public void remove(RecentItem item) {
		if (recent_msgList.contains(item)) {
			recent_msgList.remove(item);
			notifyDataSetChanged();
		}
	}

	public void addFirst(RecentItem item) {
		if (recent_msgList.contains(item)) {
			recent_msgList.remove(item);
		}
		recent_msgList.add(item);
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		return recent_msgList.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;
		convertView=null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.item07_tab3_listview_item, null);
			holder = new Holder();
        	holder.head = (ImageView)convertView.findViewById(R.id.item_list_img);
			holder.name = (TextView) convertView.findViewById(R.id.item_list_name);
			holder.info = (TextView)convertView.findViewById(R.id.item07_list_info);
			holder.num  = (TextView)convertView.findViewById(R.id.item07_num_id);
        	convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
		titleText.setText("私人消息");
		//holder.head.setBackgroundResource(FdimgArr[position]);
		//holder.text.setText(MsgText[position]);
		Bitmap bit = null;
		//String imgUrl =recent_msgList.get(position).getHeadImg() ;
		String name = recent_msgList.get(position).getName();
		String msg = recent_msgList.get(position).getMessage();
		int num = recent_msgList.get(position).getNewNum();
		if(name.equals("")){
			name = "Leone";
		}
				
//		if(imgUrl!=null){
//			bit=BitmapLoad.loadBitmap(mContext,imgUrl);
//			@SuppressWarnings("deprecation")
//			Drawable dra = new BitmapDrawable(bit);
//			holder.head.setBackgroundDrawable(dra);
//		}else{
//			Util.toast(mContext, "网络连接有误，请重试");
//			holder.head.setBackgroundResource(R.drawable.wall_bg);
//		}
		holder.name.setText(name);
		holder.info.setText(msg);
		holder.num.setText(String.valueOf(num));
		return convertView;
	}
	
	
	class Holder{
    	ImageView head;
    	TextView name;
    	TextView info;    	
    	TextView num;
    }

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();

	}
	@Override
	public void notifyDataSetInvalidated() {
		super.notifyDataSetInvalidated();


	}
	
	@Override
	public void onMessage(String message) {
		
	}

	@Override
	public void onBind(String method, int errorCode, String content) {
		
	}

	@Override
	public void onNotify(String title, String content) {
		
	}

	@Override
	public void onNetChange(boolean isNetConnected) {
		
	}				
}